#! /usr/bin/env python 
#coding=utf-8
from sqlalchemy import Column,Table,MetaData
from sqlalchemy.types import Integer,String
from sqlalchemy.orm import mapper
from util.db_utils import  Base,session
from flask_restful import fields

class User(Base):
    __tablename__ = 'user'
    id = Column('id', Integer, primary_key=True, nullable=True)
    username = Column('username', String(255))
    password = Column('password', String(255))


    resource_fields = {'id': fields.Integer, 'username': fields.String, 'password': fields.String}
    def __init__(self, kwargs):
        self.id=kwargs['id']
        self.username = kwargs['username']
        self.password = kwargs['password']
# metadata = MetaData()
#
# user = Table('user', metadata,
#             Column('id', Integer, primary_key=True),
#             Column('username', String(255)),
#             Column('password', String(255))
#         )
#
# class User(object):
#     def __init__(self, *args,**kwargs):
#         self.id=kwargs['id']
#         self.username = kwargs['username']
#         self.password = kwargs['password']
#
# mapper(User, user)

"""
查询所有的user
"""
def get_users():
    return session.query(User).all()

"""
添加user
"""
def add_user(u):
    session.add(u)
    session.commit()
    return 1

#print(type(User.__table__))


# query =session.query(User)
# for u in query.all():
#     print(u.id,u.username,u.password)
#print(query.all())
#u=User(id=3,username='python',password='python123')
#u=User()
#print(User().add_user(u))

